package net.cgsoft.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import net.cgsoft.widget.view.WheelOptions;

import java.util.ArrayList;


public class WheelOptionsPopWindow<T> extends PopupWindow {


    public static final String TAG = "OriginPopupWindow";
    TextView tvTitle;
    Button btnComplete;
    View topBody;
    Context mContext;
    private WheelOptions wheelOptions;
    OnOptionsSelectListener listener;

    public WheelOptionsPopWindow(Context context) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootView = inflater.inflate(R.layout.popup_wheel_options, null);
        setContentView(rootView);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        initView(rootView);
        addListener();
    }

    private void initView(View rootView) {
        topBody = rootView.findViewById(R.id.top_body);
        tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
        btnComplete = (Button) rootView.findViewById(R.id.btn_pop_complete);
        final View optionspicker = rootView.findViewById(R.id.optionspicker);
        wheelOptions = new WheelOptions(optionspicker);
    }

    public void showPopupWindow(View view, String title,
                                ArrayList<T> options1Items,
                                ArrayList<ArrayList<T>> options2Items) {
        if (options1Items == null || options1Items.size() == 0) {
            Toast.makeText(mContext, title + "列表为空", Toast.LENGTH_SHORT).show();
            return;
        }
        showAtLocation(view, Gravity.BOTTOM, 0, 0);
        tvTitle.setText(title);
        wheelOptions.setPicker(options1Items, options2Items, null, true);
        wheelOptions.setCyclic(false, false, false);
    }

    public void showPopupWindow(View view, String title,
                                ArrayList<T> options1Items) {
        showPopupWindow(view, title, options1Items, null);
    }

    private void addListener() {

        btnComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] optionsCurrentItems = wheelOptions.getCurrentItems();
                listener.onOptionsSelect(tvTitle.getText().toString(),
                        optionsCurrentItems[0], optionsCurrentItems[1]);
                dismiss();
            }
        });

        topBody.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setOnOptionsSelectListener(OnOptionsSelectListener listener) {
        this.listener = listener;
    }

    public interface OnOptionsSelectListener {
        void onOptionsSelect(String title, int options1, int option2);
    }
}
